package com.charity.contoller;

import com.charity.model.CommonResponse;
import com.charity.pojo.Admin;
import com.charity.pojo.User;
import com.charity.service.AdminService;
import com.charity.service.UserService;
import com.charity.utils.RSAUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class IndexController {

    public static final String PubKey = com.charity.utils.IOUtil.readResourceAsString("conf/PubKey");

    @Autowired
    private AdminService adminService;

    @Autowired
    private UserService userService;

    private CommonResponse commonResponse = new CommonResponse();

    private RSAUtil rsaUtil = new RSAUtil();

    @PostMapping("/adminLogin")
    public CommonResponse adminLogin(@RequestBody Admin admin) {
        Admin admin1 = adminService.admin(admin.getUserName(), admin.getPassword());
        if (admin1 != null) {
            return commonResponse.ok(admin1);
        }
        return commonResponse.cuw("300", "用户名或者密码错误");
    }

    @PostMapping("/userLogin")
    public CommonResponse userLogin(@RequestBody User user) {
        User user1 = userService.user(user.getUserName(), user.getPassword());
        if (user1 != null) {
            return commonResponse.ok(user1);
        }
        return commonResponse.cuw("300", "用户名或者密码错误");
    }

    @PostMapping("/addAdmin")
    public CommonResponse addAdmin(@RequestBody Admin admin) throws Exception {
        Long id = adminService.adminId(admin.getUserName());
        if (id == null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Integer result = adminService.addAdmin(admin.getUserName(), admin.getPassword(),
                    rsaUtil.encrypt(admin.getRealName(), PubKey), admin.getPhone(), admin.getEmail(), sdf.format(new Date()));
            if (result != null) {
                return commonResponse.ok(null);
            }else {
                return commonResponse.cuw("300", "系统错误");
            }
        }
        return commonResponse.cuw("300", "用户名已存在");
    }

    @PostMapping("/userRegister")
    public CommonResponse userRegister(@RequestBody User user) throws Exception {
        Long id = userService.userId(user.getUserName());
        if (user.getIdCard() == null) {
            user.setIdCard("111");
        }
        if (id == null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Integer result = userService.addUser(user.getUserName(), user.getPassword(), user.getRole(), rsaUtil.encrypt(user.getRealName(), PubKey), user.getSex(),
                    rsaUtil.encrypt(user.getIdCard(), PubKey), user.getAccount(), user.getPhone(), user.getEmail(), user.getAddress(), user.getDifficultyType(), sdf.format(new Date()));
            if (result != null) {
                return commonResponse.ok(null);
            }else {
                return commonResponse.cuw("300", "系统错误");
            }
        }
        return commonResponse.cuw("300", "用户名已存在");
    }
}
